﻿using System;
using System.Text;
using NUnit.Framework;
using Legend.Web;
using System.Web.UI;
using FakeItEasy;

namespace Legend.Core.Tests.Web
{
    [TestFixture]
    public class HtmlAttributeManagerTests
        : HtmlTextWriterExtensions.HtmlTextWriterTestBase 
    {
        [Test]
        public void renders_attributes()
        {
            this.CreateManager(a => a[HtmlTextWriterAttribute.Id, "id"][HtmlTextWriterAttribute.Name, "name"]);

            A.CallTo(() => this.writer.AddAttribute(HtmlTextWriterAttribute.Id, "id")).MustHaveHappened();
            A.CallTo(() => this.writer.AddAttribute(HtmlTextWriterAttribute.Name, "name")).MustHaveHappened();
        }

        [Test]
        public void class_renders_class_attribute()
        {
            this.CreateManager(a => a.Class("test"));
            A.CallTo(() => this.writer.AddAttribute(HtmlTextWriterAttribute.Class, "test")).MustHaveHappened();
        }

        [Test]
        public void class_called_with_multiple_class_names_renders_space_separated_class_names()
        {
            this.CreateManager(a => a.Class("test", "test2"));
            A.CallTo(() => this.writer.AddAttribute(HtmlTextWriterAttribute.Class, "test test2")).MustHaveHappened();
        }

        [Test]
        public void id_renders_id_attribute()
        {
            this.CreateManager(a => a.Id("id"));
            A.CallTo(() => this.writer.AddAttribute(HtmlTextWriterAttribute.Id, "id")).MustHaveHappened();
        }

        [Test]
        public void name_renders_name_attribute()
        {
            this.CreateManager(a => a.Name("name"));
            A.CallTo(() => this.writer.AddAttribute(HtmlTextWriterAttribute.Name, "name")).MustHaveHappened();
        }

        [Test, ExpectedException(typeof(ArgumentNullException))]
        public void throws_when_constructor_is_called_with_null_writer()
        {
            var m = new HtmlAttributeManager(null);
        }

        private void CreateManager(Func<HtmlAttributeManager, HtmlAttributeManager> attributer)
        {
            attributer(new HtmlAttributeManager(this.writer));
        }
    }
}
